package com.linzp.bishe.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linzp.bishe.common.*;
import com.linzp.bishe.controller.exception.ServiceException;
import com.linzp.bishe.entity.Accounts;
import com.linzp.bishe.excel.AccountsExcel;
import com.linzp.bishe.excel.AccountsExcelListener;
import com.linzp.bishe.model.*;
import com.linzp.bishe.service.AccountsService;
import com.linzp.bishe.service.SaveExcelData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 林泽鹏
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/accounts")
public class AccountsController {
	@Autowired
	private AccountsService accountsService;

	@Autowired
	private Catalogue catalogue;

	@Autowired
	private SaveExcelData saveExcelData;


	@Autowired
	private TokenContent tokenContent;
	/**
	 * 登录方法
	 *
	 * @param loginBody 登录信息
	 * @return 结果(com.linzp.bishe.common.Result)
	 */
	@PostMapping("/login")
	public Result login(@RequestBody LoginBody loginBody) {
		Result success = Result.success();
		//生成一个令牌
		String token = accountsService.login(loginBody.getUsername(), loginBody.getPassword(),
				loginBody.getCode(), loginBody.getUuid());
		success.put(Constants.TOKEN,token);
		return success;
	}

	/**
	 * @Description 返回用户目录
	 * @Param [httpServletRequest]
	 * @return com.linzp.bishe.common.Result
	 */
	@GetMapping("/catalogue")
	public Result getUserCatalogue(HttpServletRequest httpServletRequest){
		Accounts account = accountsService.checkAccountStatus(httpServletRequest);
		Result success = Result.success();
		List<CatalogueMessage> items;
		if (account.getRoles().equals("管理员")) {
			items = catalogue.getAdminMessage();
		}
		else{
			items = catalogue.getUserMessage();
		}
		success.put("items",items);
		return success;
	}

	/**
	 * @Description 获取用户信息
	 * @Param [httpServletRequest, currentPage, pageSize, searchName, searchRoles]
	 * @return com.linzp.bishe.common.Result
	 */
	@GetMapping("/information")
	public Result getUserInformation(HttpServletRequest httpServletRequest, Integer currentPage, Integer pageSize
			,String searchName,String searchRoles){
		accountsService.checkAccountIsAdmin(httpServletRequest);
		Result success = Result.success();
		AccountSearch search = new AccountSearch(searchName, searchRoles);
		List<AccountUser> information = accountsService.getAccountsInformation(currentPage, pageSize,search);
		int count;
		if (searchRoles != null & searchRoles.length() != 0){
			QueryWrapper<Accounts> wrapper = new QueryWrapper<>();
			wrapper.eq("roles",searchRoles);
			count = accountsService.count(wrapper);
		}
		else{
			count = accountsService.count();
		}
		success.put("account",information);
		success.put("total",count);
		return success;
	}

	/**
	 * @Description 修改账号
	 * @Param [account]
	 * @return com.linzp.bishe.common.Result
	 */
	@PutMapping("")
	public Result update(@RequestBody Accounts account){
		Result success = Result.success();
		accountsService.updateById(account);
		return success;
	}

	/**
	 * @Description 修改密码
	 * @Param [httpServletRequest, password]
	 * @return com.linzp.bishe.common.Result
	 */
	@PutMapping("/password")
	public Result updatePassword(HttpServletRequest httpServletRequest,@RequestBody String password){
		Accounts account = accountsService.checkAccountStatus(httpServletRequest);
		Result success = Result.success();
		account.setPasswords(password);
		accountsService.updateById(account);
		return success;
	}

	/**
	 * @Description 添加用户
	 * @Param [addAccount]
	 * @return com.linzp.bishe.common.Result
	 */
	@PostMapping("/addAccountUser")
	public Result addAccountUser(@RequestBody AccountUser addAccount){
		Result success = Result.success();
		accountsService.addAccountUser(addAccount);
		return success;
	}


	/**
	 * @Description 通过EXCEL真假用户
	 * @Param [file]
	 * @return com.linzp.bishe.common.Result
	 */
	@PostMapping("/addExcel")
	public Result addExcel(@RequestParam("file") MultipartFile file){
		if(file.isEmpty()){
			throw new ServiceException("没有文件",null);
		}
		if(!Constants.EXCEL.equals(file.getContentType())){
			throw new ServiceException("注意文件后缀为.xlsx",null);
		}
		Result success = Result.success();
		try {
			EasyExcel.read(file.getInputStream(), AccountsExcel.class,new AccountsExcelListener(saveExcelData)).sheet().doRead();
		} catch (IOException e) {
			throw new ServiceException("注意导入指定模板文件",null);
		}

		return success;
	}

	@GetMapping("/getSimpleAccount")
	public Result getSimpleAccount(HttpServletRequest httpServletRequest){
		Accounts accounts = accountsService.checkAccountStatus(httpServletRequest);
		Result success = Result.success();
		SimpleAccountUser user = accountsService.getSimpleAccountUser(accounts.getId());
		success.put("user",user);
		return success;
	}

	@GetMapping("/getAllSimpleAccount")
	public Result getAllSimpleAccount(Integer currentPage, Integer pageSize,String searchName,String searchRoles){
		Result success = Result.success();
		AccountSearch search = new AccountSearch(searchName, searchRoles);
		List<SimpleAccountUser> simpleAccountUser = accountsService.getAllSimpleAccountUser(currentPage,pageSize,search);
		int count = accountsService.getAllSimpleAccountUserCount(currentPage,pageSize,search);
		success.put("account",simpleAccountUser);
		success.put("total",count);
		return success;
	}

	/**
	 * @Description 退出登录
	 * @Param [token]
	 * @return com.linzp.bishe.common.Result
	 */
	@PostMapping("/logout")
	public Result logout(HttpServletRequest httpServletRequest){
		tokenContent.delUser(httpServletRequest);
		return Result.success();
	}
}


